package cfca.paperless.client.servlet._3_assist._3_05_AddWaterMarkToPdf;

import java.io.File;
import java.io.FileInputStream;

import org.apache.commons.io.FileUtils;

import cfca.paperless.client.bean.WaterMarkBean;
import cfca.paperless.client.servlet.PaperlessClient;
import cfca.paperless.client.servlet.PaperlessConfig;
import cfca.paperless.client.util.Base64;
import cfca.paperless.client.util.IoUtil;
import cfca.paperless.client.util.PaperlessUtil;

/**
 * @Description 3.5 PDF添加水印接口功能（测试用例）
 * @Author dell
 * @Date 2016-5-16
 * @CodeReviewer TODO
 */
public class AddWaterMarkToPdfTest01 {

    public static void main(String[] args) throws Exception {
        try {
            PaperlessClient clientBean = new PaperlessClient(PaperlessConfig.assistUrl, 10000, 60000);// 无纸化辅助接口的访问URL

            // 待处理的PDF文件
            String pdfFilePath = "./TestData/file.pdf";
 
            String pdfSuffix = IoUtil.getFileNameSuffix(pdfFilePath);
            if(!PaperlessConfig.PDF_FILE_SUFFIX.equals(pdfSuffix)){
                throw new Exception("所选文件不是pdf文件");
            } 
            
            byte[] pdfFileData = FileUtils.readFileToByteArray(new File(pdfFilePath));

            // 待添加的水印图片文件
            String imageFilePath = "./TestData/handwriting.png";
            
            String pngSuffix = IoUtil.getFileNameSuffix(imageFilePath);
            if(!PaperlessConfig.PNG_FILE_SUFFIX.equals(pngSuffix)){
                throw new Exception("所选图片不是png文件");
            }
            
            String imageFileData = Base64.toBase64String(FileUtils.readFileToByteArray(new File(imageFilePath)));// 水印图片Base64编码

            // 水印图片的设定信息
            String fromPage = "1";// 起始页
            String toPage = "2";// 结束页
            String absoluteX = "400";// 水印X坐标
            String absoluteY = "100";// 水印Y坐标
            String apartX = "-1";
            String apartY = "-1";
            String alpha = "0.5";// 水印透明度，取值0-1
            String fitWidth = "120";// 水印宽度
            String fitHeight = "120";// 水印高度
            String rotationDegree = "30";// 水印旋转度
            String waterMarkText = "addWaterMark2Pdf";// 水印文本
            WaterMarkBean waterMarkBean = new WaterMarkBean(fromPage, toPage, absoluteX, absoluteY, apartX, apartY, alpha, fitWidth, fitHeight, rotationDegree, imageFileData,
                    waterMarkText);

            // 操作员编码或者机构号
            String operatorCode = PaperlessConfig.operatorCode;
            // 渠道编码，可以为空
            String channelCode = PaperlessConfig.channelCode;

            // 取得添加水印后的PDF文件数据
            byte[] result = clientBean.addWaterMarkToPdf(pdfFileData, getWaterMarkStrategyXML(waterMarkBean), operatorCode, channelCode);

            String errorRsString = PaperlessUtil.isError(result);

            // 处理结果为正常，保存添加水印后的PDF文件到本地目录下
            if ("".equals(errorRsString)) {
                System.out.println("OK:addWaterMark2Pdf");
                System.out.println(result.length);
                IoUtil.write("./TestData/output/addWaterMark2Pdf-sealedPdf2.pdf", result);
            } else {
                // 处理结果为异常，打印出错误码和错误信息
                System.out.println("NG:addWaterMark2Pdf");
                System.out.println(result.length);
                System.out.println(new String(result, "UTF-8"));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 生成添加水印策略文件
     * 
     * @param waterMarkBean
     *            水印图片的设定信息
     * @return 添加水印策略文件
     */
    public static String getWaterMarkStrategyXML(WaterMarkBean waterMarkBean) {

        StringBuilder waterMarkStrategyXML = new StringBuilder();

        waterMarkStrategyXML.append("<Request>");
        waterMarkStrategyXML.append("<FromPage>").append(waterMarkBean.getFromPage()).append("</FromPage>");
        waterMarkStrategyXML.append("<ToPage>").append(waterMarkBean.getToPage()).append("</ToPage>");
        waterMarkStrategyXML.append("<AbsoluteX>").append(waterMarkBean.getAbsoluteX()).append("</AbsoluteX>");
        waterMarkStrategyXML.append("<AbsoluteY>").append(waterMarkBean.getAbsoluteY()).append("</AbsoluteY>");
        waterMarkStrategyXML.append("<ApartX>").append(waterMarkBean.getApartX()).append("</ApartX>");
        waterMarkStrategyXML.append("<ApartY>").append(waterMarkBean.getApartY()).append("</ApartY>");
        waterMarkStrategyXML.append("<Alpha>").append(waterMarkBean.getAlpha()).append("</Alpha>");
        waterMarkStrategyXML.append("<FitWidth>").append(waterMarkBean.getFitWidth()).append("</FitWidth>");
        waterMarkStrategyXML.append("<FitHeight>").append(waterMarkBean.getFitHeight()).append("</FitHeight>");
        waterMarkStrategyXML.append("<RotationDegree>").append(waterMarkBean.getRotationDegree()).append("</RotationDegree>");
        waterMarkStrategyXML.append("<WaterMarkImage>").append(waterMarkBean.getWaterMarkImage()).append("</WaterMarkImage>");
        waterMarkStrategyXML.append("<WaterMarkText>").append(waterMarkBean.getWaterMarkText()).append("</WaterMarkText>");
        waterMarkStrategyXML.append("</Request>");

        return waterMarkStrategyXML.toString();
    }

}
